import { post, remove, get, put } from './api'
import Vue from 'vue'
// import { read } from '../store/modules/init.js'

// 添加单个员工
export const add = (employeeObj, avatarFile) => {
    const url = '/usermgt/employee/form'
    // console.log(avatarFile)
    const formData = new FormData();
    formData.append('avatarFile', avatarFile);
    formData.append('method', 'add');
    formData.append('args', JSON.stringify(employeeObj));
    return Vue.http.post(url, formData, {timeout: 10000}).then((resp) => {
        if (resp.status !== 200) {
            return Promise.reject(resp)
        }
        if (_.isString(resp.body)) {
            try {
                resp.body = JSON.parse(resp.body)
            } catch (err) {
                console.log('rpcCall exception', err)
                resp.body = err
                resp.body.result = { errno: -1 }
            }
        }
        return resp
    })
}

//修改单个员工基本信息
export const update = (employeeObj, avatarFile) => {
    const url = '/usermgt/employee/form'

    const formData = new FormData();
    formData.append('avatarFile', avatarFile);
    formData.append('method', 'update');
    formData.append('args', JSON.stringify(employeeObj));
    return Vue.http.post(url, formData, {timeout: 10000}).then((resp) => {
        if (resp.status !== 200) {
            return Promise.reject(resp)
        }
        if (_.isString(resp.body)) {
            try {
                resp.body = JSON.parse(resp.body)
            } catch (err) {
                console.log('rpcCall exception', err)
                resp.body = err
                resp.body.result = { errno: -1 }
            }
        }
        return resp
    })
}

//批量修改员工组织架构
export const batchUpdate = (args) => {
    const url = `/usermgt/employee`
    const body = {
        method: 'batchUpdate',
        args: JSON.stringify(args)
    }
    return put(url, body)
}

//修改密码
export const modifyPassword = (id, employeeObj) => {
    const url = `/usermgt/usermgt/employee/${id}`
    const body = {
        method: 'modifyPassword',
        args: JSON.stringify(employeeObj)
    }
    return put(url, body)
}

//获取员工列表
export const getEmployees = (args) => {
    const url = '/usermgt/employee'
    const body = {
        method: 'getEmployees',
        args: JSON.stringify(args)
    }
    return post(url, body)
}

//查询单个员工详情
export const getEmployeeDetail = (id) => {
    const url = `/usermgt/employee/${id}`
    return get(url)
}

//根据用户名查询员工姓名
export const getEmployeeNames = (args={}) => {
    const url = '/usermgt/employee'
    const body = {
        method: 'getEmployeeNames',
        args: JSON.stringify(args)
    }
    return post(url, body)
}

// 获取所有的员工数据权限
export const getAllEmDataPerms = () => {
    const url = '/usermgt/employee'
    const body = {
        method: 'getAllEmDataPerms',
    }
    return post(url, body)
}

// 获取单个员工关联的角色和员工数据权限
export const getRolesAndEmDataPerms = (id) => {
    const url = `/usermgt/employee/${id}`
    const body = {
        method: 'getRolesAndEmDataPerms',
    }
    return post(url, body)
}


// 设置单个员工的关联角色
export const setRoles = (id, args) => {
    const url = `/usermgt/employee/${id}`
    const body = {
        method: 'assoRoles',
        args: JSON.stringify(args)
    }
    return put(url, body)
}